﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using HuaProject.DBUtility;//Please add references
namespace HuaProject.DAL
{
	/// <summary>
	/// 数据访问类:Product
	/// </summary>
	public partial class Product
	{
		public Product()
		{}
		#region  Method

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(long id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from Product");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.BigInt)
};
			parameters[0].Value = id;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public long Add(HuaProject.Model.Product model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into Product(");
			strSql.Append("Title,Numiid,Coding,Piicture,HoleSize,Power,Type,Voltage,Colors,Rating,Temperature,Material,Explain,InsertUid,InsertDate,UpdateDate)");
			strSql.Append(" values (");
			strSql.Append("@Title,@Numiid,@Coding,@Piicture,@HoleSize,@Power,@Type,@Voltage,@Colors,@Rating,@Temperature,@Material,@Explain,@InsertUid,@InsertDate,@UpdateDate)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@Title", SqlDbType.NVarChar,100),
					new SqlParameter("@Numiid", SqlDbType.NVarChar,50),
					new SqlParameter("@Coding", SqlDbType.NVarChar,50),
					new SqlParameter("@Piicture", SqlDbType.NVarChar,200),
					new SqlParameter("@HoleSize", SqlDbType.NVarChar,100),
					new SqlParameter("@Power", SqlDbType.NVarChar,100),
					new SqlParameter("@Type", SqlDbType.NVarChar,50),
					new SqlParameter("@Voltage", SqlDbType.NVarChar,500),
					new SqlParameter("@Colors", SqlDbType.NVarChar,200),
					new SqlParameter("@Rating", SqlDbType.NVarChar,100),
					new SqlParameter("@Temperature", SqlDbType.NVarChar,100),
					new SqlParameter("@Material", SqlDbType.NVarChar,200),
					new SqlParameter("@Explain", SqlDbType.NVarChar,4000),
					new SqlParameter("@InsertUid", SqlDbType.BigInt,8),
					new SqlParameter("@InsertDate", SqlDbType.DateTime),
					new SqlParameter("@UpdateDate", SqlDbType.DateTime)};
			parameters[0].Value = model.Title;
			parameters[1].Value = model.Numiid;
			parameters[2].Value = model.Coding;
			parameters[3].Value = model.Piicture;
			parameters[4].Value = model.HoleSize;
			parameters[5].Value = model.Power;
			parameters[6].Value = model.Type;
			parameters[7].Value = model.Voltage;
			parameters[8].Value = model.Colors;
			parameters[9].Value = model.Rating;
			parameters[10].Value = model.Temperature;
			parameters[11].Value = model.Material;
			parameters[12].Value = model.Explain;
			parameters[13].Value = model.InsertUid;
			parameters[14].Value = model.InsertDate;
			parameters[15].Value = model.UpdateDate;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt64(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(HuaProject.Model.Product model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update Product set ");
			strSql.Append("Title=@Title,");
			strSql.Append("Numiid=@Numiid,");
			strSql.Append("Coding=@Coding,");
			strSql.Append("Piicture=@Piicture,");
			strSql.Append("HoleSize=@HoleSize,");
			strSql.Append("Power=@Power,");
			strSql.Append("Type=@Type,");
			strSql.Append("Voltage=@Voltage,");
			strSql.Append("Colors=@Colors,");
			strSql.Append("Rating=@Rating,");
			strSql.Append("Temperature=@Temperature,");
			strSql.Append("Material=@Material,");
			strSql.Append("Explain=@Explain,");
			strSql.Append("InsertUid=@InsertUid,");
			strSql.Append("InsertDate=@InsertDate,");
			strSql.Append("UpdateDate=@UpdateDate");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@Title", SqlDbType.NVarChar,100),
					new SqlParameter("@Numiid", SqlDbType.NVarChar,50),
					new SqlParameter("@Coding", SqlDbType.NVarChar,50),
					new SqlParameter("@Piicture", SqlDbType.NVarChar,200),
					new SqlParameter("@HoleSize", SqlDbType.NVarChar,100),
					new SqlParameter("@Power", SqlDbType.NVarChar,100),
					new SqlParameter("@Type", SqlDbType.NVarChar,50),
					new SqlParameter("@Voltage", SqlDbType.NVarChar,500),
					new SqlParameter("@Colors", SqlDbType.NVarChar,200),
					new SqlParameter("@Rating", SqlDbType.NVarChar,100),
					new SqlParameter("@Temperature", SqlDbType.NVarChar,100),
					new SqlParameter("@Material", SqlDbType.NVarChar,200),
					new SqlParameter("@Explain", SqlDbType.NVarChar,4000),
					new SqlParameter("@InsertUid", SqlDbType.BigInt,8),
					new SqlParameter("@InsertDate", SqlDbType.DateTime),
					new SqlParameter("@UpdateDate", SqlDbType.DateTime),
					new SqlParameter("@id", SqlDbType.BigInt,8)};
			parameters[0].Value = model.Title;
			parameters[1].Value = model.Numiid;
			parameters[2].Value = model.Coding;
			parameters[3].Value = model.Piicture;
			parameters[4].Value = model.HoleSize;
			parameters[5].Value = model.Power;
			parameters[6].Value = model.Type;
			parameters[7].Value = model.Voltage;
			parameters[8].Value = model.Colors;
			parameters[9].Value = model.Rating;
			parameters[10].Value = model.Temperature;
			parameters[11].Value = model.Material;
			parameters[12].Value = model.Explain;
			parameters[13].Value = model.InsertUid;
			parameters[14].Value = model.InsertDate;
			parameters[15].Value = model.UpdateDate;
			parameters[16].Value = model.id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(long id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Product ");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.BigInt)
};
			parameters[0].Value = id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool DeleteList(string idlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Product ");
			strSql.Append(" where id in ("+idlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public HuaProject.Model.Product GetModel(long id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select  top 1 id,Title,Numiid,Coding,Piicture,HoleSize,Power,Type,Voltage,Colors,Rating,Temperature,Material,Explain,InsertUid,InsertDate,UpdateDate from Product ");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.BigInt)
};
			parameters[0].Value = id;

			HuaProject.Model.Product model=new HuaProject.Model.Product();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["id"].ToString()!="")
				{
					model.id=long.Parse(ds.Tables[0].Rows[0]["id"].ToString());
				}
				if(ds.Tables[0].Rows[0]["Title"]!=null)
				{
				model.Title=ds.Tables[0].Rows[0]["Title"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Numiid"]!=null)
				{
				model.Numiid=ds.Tables[0].Rows[0]["Numiid"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Coding"]!=null)
				{
				model.Coding=ds.Tables[0].Rows[0]["Coding"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Piicture"]!=null)
				{
				model.Piicture=ds.Tables[0].Rows[0]["Piicture"].ToString();
				}
				if(ds.Tables[0].Rows[0]["HoleSize"]!=null)
				{
				model.HoleSize=ds.Tables[0].Rows[0]["HoleSize"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Power"]!=null)
				{
				model.Power=ds.Tables[0].Rows[0]["Power"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Type"]!=null)
				{
				model.Type=ds.Tables[0].Rows[0]["Type"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Voltage"]!=null)
				{
				model.Voltage=ds.Tables[0].Rows[0]["Voltage"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Colors"]!=null)
				{
				model.Colors=ds.Tables[0].Rows[0]["Colors"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Rating"]!=null)
				{
				model.Rating=ds.Tables[0].Rows[0]["Rating"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Temperature"]!=null)
				{
				model.Temperature=ds.Tables[0].Rows[0]["Temperature"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Material"]!=null)
				{
				model.Material=ds.Tables[0].Rows[0]["Material"].ToString();
				}
				if(ds.Tables[0].Rows[0]["Explain"]!=null)
				{
				model.Explain=ds.Tables[0].Rows[0]["Explain"].ToString();
				}
				if(ds.Tables[0].Rows[0]["InsertUid"].ToString()!="")
				{
					model.InsertUid=long.Parse(ds.Tables[0].Rows[0]["InsertUid"].ToString());
				}
				if(ds.Tables[0].Rows[0]["InsertDate"].ToString()!="")
				{
					model.InsertDate=DateTime.Parse(ds.Tables[0].Rows[0]["InsertDate"].ToString());
				}
				if(ds.Tables[0].Rows[0]["UpdateDate"].ToString()!="")
				{
					model.UpdateDate=DateTime.Parse(ds.Tables[0].Rows[0]["UpdateDate"].ToString());
				}
				return model;
			}
			else
			{
				return null;
			}
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select id,Title,Numiid,Coding,Piicture,HoleSize,Power,Type,Voltage,Colors,Rating,Temperature,Material,Explain,InsertUid,InsertDate,UpdateDate ");
			strSql.Append(" FROM Product ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
			strSql.Append(" id,Title,Numiid,Coding,Piicture,HoleSize,Power,Type,Voltage,Colors,Rating,Temperature,Material,Explain,InsertUid,InsertDate,UpdateDate ");
			strSql.Append(" FROM Product ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "Product";
			parameters[1].Value = "id";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  Method
	}
}

